<?php

namespace App\Http\Controllers\Admin;

use App\User;
use function Couchbase\defaultDecoder;
use foo\bar;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use function Sodium\compare;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    
    public function index()
    {
        //获取轮播图表中的所有数据
        $sliders = User::paginate(5);
        //加载轮播图列表模板
        return view('admin.user.index',compact('sliders'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //加载添加轮播图模板
        return view('admin.user.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        //将post数据进行验证
        $this->validate(
            $request,
            [
                'name' => 'required',
                'password' => 'required',
            ]
        );
        //将post数据存入数据库
        $post=$request->post();
        $post["password"]=bcrypt($post["password"]);

        User::create($post);
        session()->flash('success', '添加成功');

        return redirect()->route('user.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Model\Slider  $slider
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Model\Slider  $slider
     * @return \Illuminate\Http\Response
     */
    public function edit(User $user)
    {
        //
        return view("admin.user.edit")->with("user",$user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Model\Slider  $slider
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, User $user)
    {
        //
    $user->name=$request->post("name");
    $user->email=$request->post("email");
        if($user->save())
        {
            return redirect()->route("user.index");
        }else{
            return back()->with("danger","sb");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Model\Slider  $slider
     * @return \Illuminate\Http\Response
     */
    public function destroy(User $user)
    {
        //

        if($user->delete())
        {
            return redirect()->route("user.index")->with("success","删除cg");
        }else{
            return back()->with("danger","cw");
        }

    }
}
